package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.*;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.LeaveMapper;
import cn.wolfcode.car.business.query.LeaveQuery;
import cn.wolfcode.car.business.service.ILeaveService;
import cn.wolfcode.car.business.vo.ServiceItemAuditVO;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 结算单接口
 */
@Service
@Transactional
public class LeaveServiceImpl implements ILeaveService {

    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private RepositoryService repositoryService;



    @Override
    public TablePageInfo<Leave> query(LeaveQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<Leave> leaves = leaveMapper.selectForList(qo);
        return new TablePageInfo<Leave>(leaves);
    }



    @Override
    public Leave get(Long id) {
        return leaveMapper.selectByPrimaryKey(id);
    }




    @Override
    public List<Leave> list() {
        return leaveMapper.selectAll();
    }

    @Override
    public void startAudit(ServiceItemAuditVO vo) {

        Map<String,Object> map = new HashMap<>();
        map.put("mg",vo.getDirector());
        map.put("hr",vo.getFinance());
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(vo.getBpmnInfoId());
        Leave leave = new Leave();
        leave.setName(vo.getName());
        leave.setAuditid(vo.getDirector().intValue());
        leave.setStarttime(vo.getStarttime());
        leave.setEndtime(vo.getEndtime());
        leave.setInfo(vo.getInfo());
        leave.setReason(vo.getReason());
        leave.setStatus("审核中");
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(bpmnInfo.getActProcessId(),map);
        leaveMapper.insert(leave);
    }


}












